/////////SAMPLE 1.

/////////find and install needed commands.
//////code for recoding variables to 0-1 interval
//////follow instructions that come up to install:
findit _gstd01	
//////install mediation analysis code:
///(1) download <sgmediation2.ado> from replication files from dataverse
///(2) In Stata, type "sysdir" (no quotes) in command window, hit return
///(3) Find the path/folder labeled PERSONAL
///(4) Copy <sgmediation2.ado> into this folder

/////////DEFINE VARIABLES	
///system identity threat:	
egen sitavg=rmean(sys1-sys6)
gen rsit=(sitavg-1)/6

////demographics:
gen white=1 if Race==4
replace white=0 if Race~=4
recode Gender (1=1) (2=0), generate(rsex)
gen rinc=(Income-1)/10
gen reduc=(Education-1)/4

///political information:
egen rknsum=rsum(PolKnow1_scr-PolKnow10_scr)
egen rknscal=std01(rknsum)

///authoritarianism:
gen authr2=1-auth2
egen rauthsum=rsum(auth1 authr2 auth3 auth4) 
egen rauth=std01(rauthsum)

///ideology:
gen rideo=(Ideology-1)/6

///ideological extremity:
gen ridex=abs(rideo-.5)
gen ridex01=ridex/.5

///conspiratorial thinking:
egen conthink=rmean(conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 conspthink12 ///
	conspthink13 conspthink15)
egen conthink01=std01(conthink)

///conspiracy endorsement:
egen cswami=rmean(swami1-swami15)
egen cswami01=std01(cswami)

///missing-value indicator:
egen miss=rowmiss(conthink01 cswami01 rsit age white rsex rinc reduc rauth rknscal rideo ridex)

/////////DATA ANALYSIS

//////Table 1, top panel:
egen miss=rowmiss(conthink01 cswami01 rsit age white rsex rinc reduc rauth rknscal rideo ridex)
pwcorr rsit rknscal rauth rideo ridex conthink01 cswami01 if miss==0, sig

//////Table 2:
///conspiratorial thinking
reg conthink01 rsit age white rsex rinc reduc rknscal rauth rideo ridex01, hc3
reg conthink01 rsit age white rsex rinc reduc rknscal rauth rideo ridex01, hc3 beta
display e(r2_a) 
///General CT endorsement: Swami scale
reg cswami01 rsit age white rsex rinc reduc rknscal rauth rideo ridex01, hc3
reg cswami01 rsit age white rsex rinc reduc rknscal rauth rideo ridex01, hc3 beta
display e(r2_a) 

//////Figure 1:
reg conthink01 c.rsit age white rsex rinc reduc c.rauth c.rknscal c.rideo c.ridex01, hc3
quietly margins, at(rsit=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("System Identity Threat", color(black) size(large)) ///
	xtitle("") ytitle("Conspiratorial Thinking") saving (ctk1, replace)
quietly margins, at(rauth=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Authoritarianism", color(black) size(large)) ///
	xtitle("") ytitle("Conspiratorial Thinking") saving (ctk2, replace)
quietly margins, at(rknscal=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Political Information", color(black) size(large)) ///
	xtitle("") ytitle("Conspiratorial Thinking") saving (ctk3, replace)	
quietly margins, at(rideo=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Ideology", color(black) size(large)) ///
	xtitle("") ytitle("Conspiratorial Thinking") saving (ctk4, replace)		
quietly margins, at(ridex01=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Ideological Extremity", color(black) size(large)) ///
	xtitle("") ytitle("Conspiratorial Thinking") saving (ctk5, replace)
graph combine ctk1.gph ctk3.gph ctk2.gph ctk4.gph ctk5.gph, title("Conspiratorial Thinking", color(black) size(huge)) ///
	cols(5) altshrink ycommon xsize(5) ysize(1) graphr(color(white)) saving(ctk, replace)	
reg cswami01 c.rsit age white rsex rinc reduc c.rauth c.rknscal c.rideo c.ridex01, hc3
quietly margins, at(rsit=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("System Identity Threat", color(black) size(large)) ///
	xtitle("") ytitle("General CT Endorsement") saving (gct1, replace)
quietly margins, at(rauth=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Authoritarianism", color(black) size(large)) ///
	xtitle("") ytitle("General CT Endorsement") saving (gct2, replace)
quietly margins, at(rknscal=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Political Information", color(black) size(large)) ///
	xtitle("") ytitle("General CT Endorsement") saving (gct3, replace)	
quietly margins, at(rideo=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Ideology", color(black) size(large)) ///
	xtitle("") ytitle("General CT Endorsement") saving (gct4, replace)		
quietly margins, at(ridex01=(0(.1)1))
marginsplot, recast(line) recastci(rline) plotop(lcolor(black)) ciopt(color(gs8) lp(dash))  ///
	ylabel(0 (.2) 1, gmin gmax nogex) xlabel (0 (.2) 1) graphr(c(white) lc(white)) plotr(lc(black)) ///
	title ("Ideological Extremity", color(black) size(large)) ///
	xtitle("") ytitle("General CT Endorsement") saving (gct5, replace)	
graph combine gct1.gph gct3.gph gct2.gph gct4.gph gct5.gph, title("General CT Endorsement", color(black) size(huge)) ///
	cols(5) altshrink ycommon xsize(5) ysize(1) graphr(color(white)) saving(gct, replace)
graph combine ctk.gph gct.gph, graphr(color(white)) altshrink ycommon cols(1) xsize(5) ysize(2) /// 
	saving(table2, replace)	

//////Mediation analysis (Table 5, top panel):
sgmediation2 cswami01, mv(conthink01) iv(rsit) cv(age white rsex rinc reduc rauth rknscal rideo ridex01)
bootstrap r(ind_eff) r(dir_eff) r(tot_eff), reps(10000) nodots: sgmediation2 cswami01, ///
	mv(conthink01) iv(rsit) cv(age white rsex rinc reduc rauth rknscal rideo ridex01)	
	
//////APPENDIX:
///Exploratory factor analysis on conspiratorial thinking items:
///(reverse-coded items are 2, 6, 10, 14, 16, and 17)
factor conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15 if miss==0, ipf	
factor conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15 if miss==0, ipf factors(2)
rotate, oblique oblimin(-.5)	
	
///Confirmatory factor analyses on dependent variables (2 factors vs 1 factor), with error covariances:
quietly sem (GCT -> swami1-swami15) ///
	(CTS -> conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15) if miss==0, cov(e.swami7*e.swami10) ///
	cov(e.conspthink4*e.conspthink13) ///
	cov(e.conspthink16rev*e.conspthink17rev) ///
	cov(e.conspthink6rev*e.conspthink10rev) ///
	cov(e.conspthink10rev*e.conspthink14rev) ///
	cov(e.conspthink7*e.conspthink8) ///
	cov(e.conspthink6rev*e.conspthink14rev) ///
	cov(e.conspthink1*e.conspthink3) ///
	cov(e.swami4*e.swami6) ///
	cov(e.conspthink7*e.conspthink15)
estat gof, stats(chi2 rmsea res ic indices)
quietly sem (ALL -> swami1-swami15 conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15) if miss==0, cov(e.swami7*e.swami10) ///
	cov(e.conspthink4*e.conspthink13) ///
	cov(e.conspthink16rev*e.conspthink17rev) ///
	cov(e.conspthink6rev*e.conspthink10rev) ///
	cov(e.conspthink10rev*e.conspthink14rev) ///
	cov(e.conspthink7*e.conspthink8) ///
	cov(e.conspthink6rev*e.conspthink14rev) ///
	cov(e.conspthink1*e.conspthink3) ///
	cov(e.swami4*e.swami6) ///
	cov(e.conspthink7*e.conspthink15)
estat gof, stats(chi2 rmsea res ic indices)

///Confirmatory factor analyses on dependent variables (2 factors vs 1 factor), no error covariances:
quietly sem (GCT -> swami1-swami15) ///
	(CTS -> conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15) if miss==0
estat gof, stats(chi2 rmsea res ic indices)
quietly sem (ALL -> swami1-swami15 conspthink2rev conspthink6rev conspthink10rev conspthink14rev conspthink16rev conspthink17rev ///
	conspthink1 conspthink3 conspthink4 conspthink5 conspthink7 conspthink8 conspthink9 conspthink11 ///
	conspthink12 conspthink13 conspthink15) if miss==0
estat gof, stats(chi2 rmsea res ic indices)	

///Exploratory factor analysis on system identity threat items:
factor sys1 sys2 sys3 sys4 sys5 sys6 if miss==0, ipf	
